package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.modular.entity.Reserved;
import cn.stylefeng.guns.modular.entity.Seat;
import cn.stylefeng.guns.modular.mapper.SeatMapper;
import cn.stylefeng.guns.modular.service.ReservedService;
import cn.stylefeng.guns.modular.service.SeatService;
import cn.stylefeng.guns.modular.vo.SeatInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李宝嘉
 * @Date 2022/1/1 14:20
 */
@Service
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat> implements SeatService {

    @Resource
    private ReservedService reservedService;

    @Override
    public List<SeatInfoVO> getSeatListByRoomId(Long roomId, String date) {
        LambdaQueryWrapper<Seat> seatQueryWrapper = new LambdaQueryWrapper<>();
        seatQueryWrapper.eq(Seat::getRoomId, roomId);
        List<Seat> seatList = this.list(seatQueryWrapper);
        List<Reserved> reservedList = reservedService.getListByBeginTime(date);
        List<SeatInfoVO> seatInfoVOS = new ArrayList<>();

        seatList.forEach(seat -> {
            SeatInfoVO seatInfoVO = new SeatInfoVO();
            seatInfoVO.setId(seat.getId());
            seatInfoVO.setSeatNum(String.format("%03d", seat.getNumber()));
            seatInfoVO.setCanReserved(seat.isCanReserved());
            seatInfoVO.setReservedList(reservedList.stream()
                    .filter(reserved -> reserved.getSeatId().equals(seat.getId()))
                    .collect(Collectors.toList()));
            seatInfoVOS.add(seatInfoVO);
        });
        return seatInfoVOS;
    }
}
